﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace IT_Pro.Class
{
   class MT_DanhMucLinhKien
    {
        private string _maLinhKien;

        public string MaLinhKien
        {
            get { return _maLinhKien; }
            set { _maLinhKien = value; }
        }
        private string _tenLinhKien;

        public string TenLinhKien
        {
            get { return _tenLinhKien; }
            set { _tenLinhKien = value; }
        }

        public static bool ThemLinhKien(MT_DanhMucLinhKien _linhKien)
        {
           
            string sql = "select * from MT_DanhMucLinhKien where MaLK='" + _linhKien.MaLinhKien + "'";
            DataTable dt = DataServices.ExecQuery(sql);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            sql = "insert into MT_DanhMucLinhKien values ('" + _linhKien.MaLinhKien + "',N'" + _linhKien.TenLinhKien + "')";
            DataServices.ExecNonQuery(sql);
            return true;
            
            /*
             string  sql = "insert into MT_DanhMucLinhKien values ('" + _linhKien.MaLinhKien + "',N'" + _linhKien.TenLinhKien + "')";
             
            string[] strsql = new string[1];
            strsql[0] = sql;
            DataServices.ExecuteSqlTransaction(strsql);
            return true;
             */

            // tets tran 2
            /*
            string sql = "insert into MT_DanhMucLinhKien values ('" + _linhKien.MaLinhKien + "',N'" + _linhKien.TenLinhKien + "')";
            DataServices.OpenConnection();
            SqlCommand command = DataServices.connection.CreateCommand();
            SqlTransaction transaction = DataServices.connection.BeginTransaction();
            command.Connection = DataServices.connection;
            command.Transaction = transaction;
            try
            {
                command.CommandText = sql;
                command.ExecuteNonQuery();
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();

            }
            DataServices.OpenConnection();
            return true;
            */
        }

        public static void SuaLinhKien(MT_DanhMucLinhKien _linhKien)
        {
            string sql = "update MT_DanhMucLinhKien set TenLK=N'" + _linhKien.TenLinhKien + "' where MaLK='" + _linhKien.MaLinhKien + "'";
            DataServices.ExecNonQuery(sql);
        }

        public static DataTable Hienthids(string _maLinhKien)
        {
            string sql = "select * from MT_DanhMucLinhKien where MaLK='" + _maLinhKien + "'";
            return DataServices.ExecQuery(sql);
        }

        public static void XoaLinhKien(string _maLinhKien)
        {

            string sql = "delete MT_DanhMucLinhKien where MaLK='" + _maLinhKien + "'";
            DataServices.ExecNonQuery(sql);
        }

        public static DataTable HienthidsLK()
        {
            string sql = "select 0 as STT,* from MT_DanhMucLinhKien";
            return DataServices.ExecQuery(sql);
        }
    }
}
